package club.yuit.desktop.ui.topics;

import club.yuit.rmq.model.TopicDetails;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.util.Set;

import static club.yuit.desktop.ui.topics.TopicListControlPanel.*;

/**
 * @author: yuit
 * @date: 2023/12/08 14:45
 */
public class TopicCardPanel extends JPanel {

    private final TopicDetails details;
    private final Color retryColor = Color.YELLOW;
    private final Color delqColor = Color.BLACK;
    private final Color sysColor = Color.RED;

    private final Color defaultColor = Color.GRAY;

    private LineBorder border;


    private TopicCardListPanel listPanel;

    // 默认为普通topic
    private int value = 0b1000;

    public TopicCardPanel(TopicDetails details, TopicCardListPanel listPanel) {
        this.details = details;
        border = new LineBorder(getColorByType(details.getName()), 2, true);
        this.listPanel = listPanel;
        this.setBorder(border);
        BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
        this.setName(details.getName());
        setLayout(layout);
        this.setMinimumSize(new Dimension(40, 40));
        this.add(topicPanel());
        this.add(clusterInfo(details.getCluster()));
        this.add(new JSeparator());
        this.add(optionPanel());
        initShowValue();
    }


    private void initShowValue() {
        String name = details.getName();
        if (name.startsWith("%SYS%")) {
            value = SYS_TYPE;
        }

        if (name.startsWith("%RETRY%")) {
            value = RETRY_TYPE;
        }

        if (name.startsWith("%DLQ%")) {
            value = DLQ_TYPE;
        }
    }


    public boolean isShow(int showValue) {
        int f = showValue & value;
        return f > 0;
    }


    public JPanel topicPanel() {
        String name = details.getName();
        if (name.startsWith("%SYS%")){
           name= name.substring(5);
        }

        JLabel topic = new JLabel(name);

        topic.putClientProperty("FlatLaf.styleClass", "font: 200% $light.font");
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));

        panel.add(topic);

        return panel;
    }

    private JPanel clusterInfo(Set<String> cluster) {
        JLabel title = new JLabel("cluster:");
        title.setForeground(Color.GRAY);
        String clusterStr = "[" + String.join(",", cluster) + "]";
        JLabel info = new JLabel(clusterStr);
        info.putClientProperty("FlatLaf.styleClass", "mini");
        info.setForeground(Color.GRAY);
        FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
        JPanel panel = new JPanel(flowLayout);
        panel.add(title);
        panel.add(info);
        // panel.setBorder(new LineBorder(Color.GRAY, 1, true));
        return panel;

    }

    private JPanel optionPanel() {
        JButton producer = new JButton("发消息");
        if (details.getName().startsWith("%SYS%")){
            producer.setEnabled(false);
        }

        JButton more = new JButton("更多");
        more.addActionListener(e->{
            JPopupMenu menu = listPanel.getMenu();
            if (!menu.isShowing()){
                listPanel.setCurrentTopic(details.getName());
                menu.show(more, 0, more.getHeight());
            }
        });
        JPanel panel = new JPanel();
        FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT, 5, 10);
        panel.setLayout(flowLayout);
        panel.add(producer);
        panel.add(more);

        return panel;
    }


    @Override
    public Insets getInsets() {
        return new Insets(5, 5, 5, 5);
    }


    private Color getColorByType(String name) {
        if (name.startsWith("%DLQ%")) {
            return delqColor;
        } else if (name.startsWith("%RETRY%")) {
            return retryColor;
        } else if (name.startsWith("%SYS%")) {
            return sysColor;
        } else {
            return defaultColor;
        }
    }

    public TopicDetails getDetails() {
        return details;
    }
}
